package com.baomidou.springwind.controller.supply;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.springwind.common.business.network;
import com.baomidou.springwind.controller.BaseController;
import com.baomidou.springwind.entity.*;
import com.baomidou.springwind.mapper.*;
import com.baomidou.springwind.pojo.SiteSelection;
import com.baomidou.springwind.service.NetworkCostService;
import com.baomidou.springwind.service.NetworkSupplyService;
import com.baomidou.springwind.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * <p>
 * 物流网络规划:目前和优化的对比分析
 * </p>
 *
 * @Author huangtao
 * @Date 2017-8-21
 */
@Controller
@RequestMapping("/network/supply")
public class CompareController extends BaseController {

    @Autowired
    private NetworkSupplyService networkSupplyService;

    @Autowired
    private NetworkSupplyDao networkSupplyDao;

    @Autowired
    private ProjectService projectService;
    @Autowired
    private NetworkCostService networkCostService;
    @Autowired
    private NetworkReliabilityDao networkReliabilityDao;
    @Autowired
    private NetworkDepotDao networkDepotDao;
    @Autowired
    private NetworkDepotGoodsDao networkDepotGoodsDao;
    @Autowired
    private NetworkAlgorithmStateDao networkAlgorithmStateDao;
    /**
     * 成本
     */
    @RequestMapping("/compare")
    @Permission("1001")
    public String response(Model model){
        return pjaxPath("/lpp/network/supply/compare/main", model);
    }

    /**
     * 反应性数据展示
     * @return
     */
    @ResponseBody
    @RequestMapping("/compare/getAllData")
    @Permission("1001")
    public String getAllData() {
        // 0 是优化后的  1是目前现状的
        JSONObject ret = new JSONObject();
        EntityWrapper<NetworkCost> wrapper1 = new EntityWrapper<NetworkCost>();
        wrapper1.eq("batch_id", getProjectId());
        wrapper1.eq("type",1);
        wrapper1.eq("type2",0);
        wrapper1.orderBy("type1");
        List<NetworkCost> list1= networkCostService.selectList(wrapper1);
        EntityWrapper<NetworkCost> wrapper2 = new EntityWrapper<NetworkCost>();
        wrapper2.eq("batch_id", getProjectId());
        wrapper2.eq("type",1);
        wrapper2.eq("type2",1);
        wrapper2.orderBy("type1");
        List<NetworkCost> list2= networkCostService.selectList(wrapper2);
        if(list1.size()==0){
            return callbackFail("请先进行目前成本分析");
        }else if(list2.size()==0){
            return callbackFail("请先进行优化成本分析");
        }else{
            ret=build(list1,list2);
        }
        return callbackSuccess(ret);
    }
    public JSONObject build(List<NetworkCost> list1,List<NetworkCost> list2){
        JSONObject ret = new JSONObject();

        JSONArray array1 = new JSONArray();
        array1.add("优化分析");
        array1.add("目前分析");
        ret.put("arr1",array1);
        //第一个图
        double sum11=0;
        double sum21=0;
        JSONArray array2 = new JSONArray();
        JSONObject object11 = JSONObject.parseObject(list1.get(0).getJson());
        for(HashMap.Entry<String,Object> entry:object11.entrySet()){
            sum11=sum11+Double.parseDouble(entry.getValue().toString());
        }
        array2.add(sum11);
        JSONObject object21 = JSONObject.parseObject(list2.get(0).getJson());
        for(HashMap.Entry<String,Object> entry:object21.entrySet()){
            sum21=sum21+Double.parseDouble(entry.getValue().toString());
        }
        array2.add(sum21);
        ret.put("arr2",array2);
        //第二个图
        ret.put("arr3",array1);
        double sum12=0;
        double sum22=0;
        JSONArray array4 = new JSONArray();
        JSONObject object12 = JSONObject.parseObject(list1.get(1).getJson());
        for(HashMap.Entry<String,Object> entry:object12.entrySet()){
            sum12=sum12+Double.parseDouble(entry.getValue().toString());
        }
        array4.add(sum12);
        JSONObject object22 = JSONObject.parseObject(list2.get(1).getJson());
        for(HashMap.Entry<String,Object> entry:object22.entrySet()){
            sum22=sum22+Double.parseDouble(entry.getValue().toString());
        }
        array4.add(sum22);
        ret.put("arr4",array4);
        //第3个图
        ret.put("arr5",array1);
        double sum13=0;
        double sum23=0;
        JSONArray array6 = new JSONArray();
        JSONObject object13 = JSONObject.parseObject(list1.get(2).getJson());
        for(HashMap.Entry<String,Object> entry:object13.entrySet()){
            JSONObject object131 = JSONObject.parseObject(entry.getValue().toString());
            for(HashMap.Entry<String,Object> entry2:object131.entrySet()){
                sum13=sum13+Double.parseDouble(entry2.getValue().toString());
            }
        }
        JSONObject object130 = new JSONObject();
        object130.put("name","目前");
        object130.put("value",sum13);
        array6.add(object130);
        JSONObject object23 = JSONObject.parseObject(list2.get(2).getJson());
        for(HashMap.Entry<String,Object> entry:object23.entrySet()){
            JSONObject object131 = JSONObject.parseObject(entry.getValue().toString());
            for(HashMap.Entry<String,Object> entry2:object131.entrySet()){
                sum23=sum23+Double.parseDouble(entry2.getValue().toString());
            }
        }
        JSONObject object230 = new JSONObject();
        object230.put("name","优化");
        object230.put("value",sum23);
        array6.add(object230);
        ret.put("arr6",array6);
        //第四个图
        ret.put("arr7",array1);
        double sum14=0;
        double sum24=0;
        JSONArray array8 = new JSONArray();
        JSONObject object14 = JSONObject.parseObject(list1.get(3).getJson());
        for(HashMap.Entry<String,Object> entry:object14.entrySet()){
            sum14=sum14+Double.parseDouble(entry.getValue().toString());
        }
        array8.add(sum14);
        JSONObject object24 = JSONObject.parseObject(list2.get(3).getJson());
        for(HashMap.Entry<String,Object> entry:object24.entrySet()){
            sum24=sum24+Double.parseDouble(entry.getValue().toString());
        }
        array8.add(sum24);
        ret.put("arr8",array8);
        //第五图
        ret.put("arr9",array1);
        double sum15=0;
        double sum25=0;
        JSONArray array10 = new JSONArray();
        JSONObject object15 = JSONObject.parseObject(list1.get(4).getJson());
        for(HashMap.Entry<String,Object> entry:object15.entrySet()){
            sum15=sum15+Double.parseDouble(entry.getValue().toString());
        }
        array10.add(sum15);
        JSONObject object25 = JSONObject.parseObject(list2.get(4).getJson());
        for(HashMap.Entry<String,Object> entry:object25.entrySet()){
            sum25=sum25+Double.parseDouble(entry.getValue().toString());
        }
        array10.add(sum25);
        ret.put("arr10",array8);
        //第6个图
        ret.put("arr11",array1);
        double sum16=0;
        double sum26=0;
        JSONArray array12 = new JSONArray();
        JSONObject object16 = JSONObject.parseObject(list1.get(5).getJson());
        for(HashMap.Entry<String,Object> entry:object16.entrySet()){
            JSONObject object131 = JSONObject.parseObject(entry.getValue().toString());
            for(HashMap.Entry<String,Object> entry2:object131.entrySet()){
                sum16=sum16+Double.parseDouble(entry2.getValue().toString());
            }
        }
        JSONObject object160 = new JSONObject();
        object160.put("name","目前");
        object160.put("value",sum16);
        array12.add(object160);
        JSONObject object26 = JSONObject.parseObject(list2.get(5).getJson());
        for(HashMap.Entry<String,Object> entry:object26.entrySet()){
            JSONObject object131 = JSONObject.parseObject(entry.getValue().toString());
            for(HashMap.Entry<String,Object> entry2:object131.entrySet()){
                sum26=sum26+Double.parseDouble(entry2.getValue().toString());
            }
        }
        JSONObject object260 = new JSONObject();
        object260.put("name","优化");
        object260.put("value",sum26);
        array12.add(object260);
        ret.put("arr12",array12);
        //第七图
        ret.put("arr13",array1);
        double sum17=0;
        double sum27=0;
        JSONArray array14 = new JSONArray();
        JSONObject object17 = JSONObject.parseObject(list1.get(6).getJson());
        for(HashMap.Entry<String,Object> entry:object17.entrySet()){
            sum17=sum17+Double.parseDouble(entry.getValue().toString());
        }
        array14.add(sum17);
        JSONObject object27 = JSONObject.parseObject(list2.get(6).getJson());
        for(HashMap.Entry<String,Object> entry:object27.entrySet()){
            sum27=sum27+Double.parseDouble(entry.getValue().toString());
        }
        array14.add(sum27);
        ret.put("arr14",array14);
        //第八图
        ret.put("arr15",array1);
        double sum18=0;
        double sum28=0;
        JSONArray array16 = new JSONArray();
        JSONObject object18 = JSONObject.parseObject(list1.get(7).getJson());
        for(HashMap.Entry<String,Object> entry:object18.entrySet()){
            sum18=sum18+Double.parseDouble(entry.getValue().toString());
        }
        array16.add(sum18);
        JSONObject object28 = JSONObject.parseObject(list2.get(7).getJson());
        for(HashMap.Entry<String,Object> entry:object28.entrySet()){
            sum28=sum28+Double.parseDouble(entry.getValue().toString());
        }
        array16.add(sum28);
        ret.put("arr16",array16);
        //第九图
        ret.put("arr17",array1);
        double sum19=0;
        double sum29=0;
        JSONArray array18 = new JSONArray();
        JSONObject object19 = JSONObject.parseObject(list1.get(8).getJson());
        for(HashMap.Entry<String,Object> entry:object19.entrySet()){
            sum19=sum19+Double.parseDouble(entry.getValue().toString());
        }
        array18.add(sum19);
        JSONObject object29 = JSONObject.parseObject(list2.get(8).getJson());
        for(HashMap.Entry<String,Object> entry:object29.entrySet()){
            sum29=sum29+Double.parseDouble(entry.getValue().toString());
        }
        array18.add(sum29);
        ret.put("arr18",array18);

        return ret;
    }



}
